package com.gitee.wsl.ext.array

import androidx.compose.ui.util.fastForEach
import com.gitee.wsl.ext.base.Arrays.arraycopy


/**
 * Copied from korlibs
 */
fun List<ByteArray>.join(): ByteArray = _join(
    this,
    { ByteArray(it) },
    { it.size },
    { a, b, c, d, e -> arraycopy(a, b, c, d, e) }
)
internal inline fun <TArray> _join(
    items: List<TArray>,
    build: (Int) -> TArray,
    size: (TArray) -> Int,
    arraycopy: (TArray, Int, TArray, Int, Int) -> Unit
): TArray {
    val out = build(items.sumOf { size(it) })
    var pos = 0
    items.fastForEach { c ->
        arraycopy(c, 0, out, pos, size(c))
        pos += size(c)
    }
    return out
}